//Christian Larco, Assignment 7, LCD temperature 

//----------------- Libraries that we are going to use for the program --------------------------------
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/pgmspace.h>
//-----------------------------------------------------------------------------------------------------

//-------------------Header file for the LCD ---------------------------------------------------------- 
#include "nokia5110.h"			

//------------------ Functions used in the program ---------------------------------------------------
void init();	// Function to initialize values
int convert();	// Function for the conversion of the code
//----------------------------------------------------------------------------------------------------

//------------------  This is the main body of the program -------------------------------------------
int main()
{
	init();
	
	uint16_t divOneTemp;			// Stores the unit value of the temperature
	uint16_t divTenthTemp;			// Stores the tenths of the number 
	uint16_t temp;					// This is the variable to store the temperature
	uint16_t divTenTemp; 			// Storest the ten value of the value read
	
	// ----------------------- While loop to read in value, convert and display ----------------------
	while (1)
	{
		temp = convert();				// Read the temperature
		
		divTenTemp = temp%10;			// Variables to save different numbers
		divOneTemp = temp%100/10;		
		divTenthTemp = temp%1000/100;	
		
		char TemperatureArray[4];		// Array to dispay all tem numbers
		
		sprintf(TemperatureArray, "%d%d.%d ", divTenTemp, divOneTemp, divTenthTemp);
		
		LcdClear();						// Clears the LCD from everything
		
		LcdGotoXYFont(1,1);				// This is the column 1 and row 1
		LcdFStr(FONT_1X,(unsigned char*)PSTR ("Christian L."));
		
		LcdGotoXYFont(1,2);				// This is the Column 1 and Row 2
		LcdFStr(FONT_1X,(unsigned char*)PSTR ("Fahrenheit"));
		
		LcdGotoXYFont(1,3);				// This is the Column 1 and Row 3
		LcdFStr(FONT_1X,(unsigned char*)TemperatureArray);
		
		LcdUpdate();				// This command updates the LCD
		_delay_ms(500);				// Give it a delay so it can display for some time
	}
	return 0;
}

// ---------------------- Code to initialize the LCD and the clock ---------------------------
void init()
{
	LcdInit();				// Initialize LCD from nokia5110.h
	LcdContrast(0x7F);			// Set highest contrast for maximum visibility
	LcdClear();			// Clear LCD and enable updating
	ADCSRA = 0x87;			// ADC Enable with Clock/128
	ADMUX = 0x60;			// AVCC VRef with ADC0 for input
	DDRC = 0x00;			// PortC as an input
}
// -------------------------------------------------------------------------------------------

// ---------------------- Code to convert the values being read -------------------------------
int convert()
{
	uint16_t temp;			// temp variable
	ADCSRA = 0xE7;			// Set ADSC to 1 and Start conversion
	while( !(ADCSRA & (1<<ADSC)) == 0);	// Sit for full conversion
	temp = (ADCH << 8)|ADCL;		// Save temperature
	return temp;
}
// -------------------------------------------------------------------------------------------
